#include <stdio.h>
#include <string.h>
#include <stdlib.h>

size_t my_strlen(const char *s)
{
    size_t len = 0;

    if(NULL == s)
    {
        return 0;
    }

    while(*s != '\0')
    {
        len++;
        s++;
    }

    return len;
}

char * my_strcpy(char *dest, char *src)

{
    if(dest == NULL || src == NULL)
    {
        return NULL;
    }
    
    char *temp = dest;
    while(*src != 0)
    {
        *dest = *src;
        dest++;
        src++;
    }

    *dest = '\0';
    dest = temp;
    return dest;
}

char * my_strncpy(char *dest, char *src, size_t n)
{
    if(dest == NULL || src == NULL)
    {
        return NULL;
    }
    int i;
    char *temp = dest;
    for(i = 0; (i < n) && (*src != '\0'); i++)
    {
        *temp = *src;
        temp++;
        src++;
    }

    for(; i<n; i++)
    {
        *temp = '\0';
        temp++;
    }
    //*temp = '\0';
    return dest;
}

char * my_strcat(char *dest, char *src)
{
    if(dest ==NULL || src == NULL)
    {
        return NULL;
    }

    char *temp =dest;

    while(*temp != '\0')
    {
        temp++;
    }

    while(*src != '\0')
    {
        *temp = *src;
        temp++;
        src++; 
    }

    *temp = '\0';
    return dest;
}

char * my_strncat(char *dest, char *src, size_t n)
{
    if(dest ==NULL || src == NULL)
    {
        return NULL;
    } 

    char *temp = dest;
    while(*temp != '\0')
    {
        temp++;
    }

    for(int i = 0; (i < n) && (*src != '\0'); i++)
    {
        *temp = *src;
        temp++;
        src++;
    }

    *temp = '\0';
    return dest;
}

int my_strcmp(char *s1,char *s2)
{
    if(s1 == NULL && s2 != NULL)
    {
        return -1;
    }

    if(s1 != NULL && s2 == NULL)
    {
        return 1;
    }

    if(s1 == NULL && s2 == NULL)
    {
        return 0;
    }

    while(*s1 != '\0' && *s2 != '\0')
    {
        if(*s1 > *s2 )
        {
            return -1;
        }

        s1++;
        s2++;
    }

    if(*s1 == '\0' && *s2 == '\0') 
    {
        return 0;
    }

    if(*s1 != '\0' && *s2 == '\0')
    {
        return 1;
    }
    if(*s1 == '\0' && *s2 != '\0')
    {
        return -1;
    }   
}

int my_strncmp(char *s1,char *s2,size_t n)
{
    if(*s1 == NULL || *s2 == NULL)
    {
        return -1;
    }

    for(int i = 0; (i < n) && (s1[i] !='\0') && (s2[i] != '\0'); i++)
    {
        if(s1[i] != s2[i])
        {
            return -1;
        }
    }


    return 0;
}

int main()
{
    char *ptr = "hello world"; //保存在数据段的or段内；该数据不能被修改

    char src[100] = "hello world";

    char *dest = (char *)malloc(sizeof(char) * 100);
    if(NULL == dest)
    {
        printf("malloc error!\n");
        exit(1);
    }

    //strcpy(dest, "hello");

    size_t len = my_strlen(src);
    size_t len1 = strlen("hello world");

    printf("len = %ld\n", len);
    //printf("len1 = %ld\n", strlen(src));

    //char *temp = my_strcpy(dest, src);//strcpy_s(char *dest, int len, char *src)
    // char *temp = strncpy(dest, src, 5);

    // if(NULL == temp)
    // {
    //     printf("my_strcpy is error!\n");
    //     exit(1);
    // }
 #if 0   
    char *temp = my_strncat(dest, src,3);

    printf("dest = %s\n", dest);
    printf("temp = %s\n", temp);

    printf("dest = %p\n", dest);
    printf("temp = %p\n", temp);
#endif

    strcpy(dest, "hello");
    int ret = my_strncmp(dest,src,my_strlen(dest));

    printf("ret = %d\n",ret);

    return 0;
}